/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.modules.base.service.AbsBizObjCacheService.java 
 * <p>
 * 类产生时间: 2014-4-30 上午 10:49
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.econsole.modules.base.service;

import com.oceansoft.mobile.econsole.cache.memcache.CacheManager;
import com.oceansoft.mobile.econsole.cache.memcache.CacheManagerFactory;
import com.oceansoft.mobile.econsole.cache.memcache.CacheUtils;
import com.oceansoft.mobile.econsole.common.spring.SpringContextHolder;
import com.oceansoft.mobile.econsole.modules.base.biz.AssertUtils;
import com.oceansoft.mobile.econsole.modules.base.biz.IBizObjCache;
import com.oceansoft.mobile.econsole.modules.base.biz.IBizObjSerializable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.*;

/**
 * 业务数据缓存抽象服务类
 *
 * @author: Andy
 * @time: 2014-4-30 上午 10:49
 */
public class AbsBizObjCacheService implements IBizObjCache {
    protected Log log = null;

    @Resource
    private HttpServletRequest request;

    public AbsBizObjCacheService() {
        log = LogFactory.getLog(this.getClass());
    }

    public CacheManager getMemCacheManager() {
        CacheManager cm = CacheManagerFactory.getInstance().getMemCacheManager();
        if (cm == null) {
            throw new ExceptionInInitializerError("Cache Manager Initial Error,Please Check cache config.");
        }
        return cm;
    }

    @Override
    public <T extends IBizObjSerializable> void setInCache(T t) {
        Assert.notNull(t);
        this.getMemCacheManager().set(CacheUtils.keyOfClass(t.getClass(), t.getId()), t);
    }

    @Override
    public <T extends IBizObjSerializable> void deleteFromCache(T t) {
        Assert.notNull(t);
        AssertUtils.notNewBusinessObject(t);
        this.getMemCacheManager().delete(CacheUtils.keyOfObject(t, t.getId()));
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T extends IBizObjSerializable> T getFromCache(Class<T> clazz, Serializable id) {
        Assert.notNull(clazz);
        Assert.notNull(id);
        IBizObjSerializable obj = (IBizObjSerializable) this.getMemCacheManager().get(CacheUtils.keyOfClass(clazz, id));
        return (T) obj;
    }

    @Override
    public <T extends IBizObjSerializable> Set<T> filterNewBizObj(List<T> ts) {
        if (CollectionUtils.isEmpty(ts)) {
            return null;
        }
        Set<T> set = new HashSet<T>();
        for (T t : ts) {
            if (!t.isNew()) {
                set.add(t);
            }
        }
        return set;
    }

    @Override
    public <T extends IBizObjSerializable> Set<T> filterNotNewBizObj(List<T> ts) {
        if (CollectionUtils.isEmpty(ts)) {
            return null;
        }
        Set<T> set = new HashSet<T>();
        for (T t : ts) {
            if (t.isNew()) {
                set.add(t);
            }
        }
        return set;
    }

    @Override
    public <T extends IBizObjSerializable> List<java.io.Serializable> getBizObjIds(List<T> bos) {
        if (!CollectionUtils.isEmpty(bos)) {
            List<java.io.Serializable> ids = new ArrayList<Serializable>();
            for (T bo : bos) {
                if (!bo.isNew()) {
                    ids.add(bo.getId());
                }
            }
            return ids;
        }
        return null;
    }
    //    protected AppConfiguration getAppConfig() {
    //        return AppConfiguration.getInstance();
    //    }

    protected String getMessage(String code) {
        return this.getMessage(code, new Object[]{});
    }

    protected String getMessage(String code, Object arg0) {
        return getMessage(code, new Object[]{arg0});
    }

    protected String getMessage(String code, Object arg0, Object arg1) {
        return getMessage(code, new Object[]{arg0, arg1});
    }

    protected String getMessage(String code, Object arg0, Object arg1, Object arg2) {
        return getMessage(code, new Object[]{arg0, arg1, arg2});
    }

    protected String getMessage(String code, Object arg0, Object arg1, Object arg2, Object arg3) {
        return getMessage(code, new Object[]{arg0, arg1, arg2, arg3});
    }

    protected String getMessage(String code, Object[] args) {
        try {
            LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
            Locale locale = localeResolver.resolveLocale(request);
            return SpringContextHolder.getApplicationContext().getMessage(code, args, locale);
        } catch (Exception e) {
            log.error(e);
            return code;
        }
    }
}
